package com.ym.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 处理html有关的东西
 *
 * @author: 李涛
 * @version: 2020年02月21日 13:27
 */
public class HtmlHandleUtil {

	/**
	 * 要替换宽度的标签
	 */
	private static final String[] basicTag = {
			"img", "p", "table", "a", "tr", "td", "dd", "div", "input", "label", "li", "span", "th", "tbody", "ul"
	};

	/**
	 * 行内元素
	 */
	private static final String[] rowTags = {
			"span", "a"
	};

	/**
	 * 移除markdown标签
	 *
	 * @param content
	 * @return
	 */
	public static String removeMarkdownTag(String content) {
		if (StringUtil.isBlank(content)) {
			return content;
		}
		content = content.replace("figure", "div");
		content = content.replace("data-tool=\"mdnice编辑器\"", "");
		return content;
	}


	/**
	 * 给定宽度，处理富文本中图片的宽度
	 *
	 * @param content
	 * @param width
	 * @return
	 */
	public static String resize(String content, int width) {
		if (StringUtil.isBlank(content)) {
			return content;
		}
		Document parse = Jsoup.parse(content);
		Element body = parse.body();
		resize(body, width, basicTag);
		content = body.toString();
		content = content.replace("figure", "div");
		content = content.replace("data-tool=\"mdnice编辑器\"", "");
		return content.replaceAll("<body.*>|</body>", "").replaceAll("\\n", "");
	}

	/**
	 * 给定宽度，处理富文本中图片的宽度
	 *
	 * @param content
	 * @param width
	 * @param force   强制设置宽度
	 * @return
	 */
	public static String resize(String content, int width, boolean force) {
		if (org.jsoup.helper.StringUtil.isBlank(content)) {
			return content;
		}
		Document parse = Jsoup.parse(content);
		Element body = parse.body();
		resize(body, width, force, basicTag);
		content = body.toString();
		content = content.replace("figure", "div");
		content = content.replace("data-tool=\"mdnice编辑器\"", "");
		return content.replaceAll("<body.*>|</body>", "").replaceAll("\\n", "");
	}

	/**
	 * 设置宽度
	 *
	 * @param e
	 * @param width   要设置的宽度
	 * @param tagName
	 */
	private static void resize(Element e, int width, boolean force, String... tagName) {
		// 不包含，获取子类继续
		Elements allElements = e.children();
		for (Element subE : allElements) {
			String subTagName = subE.tagName();
			if (StringUtils.containsAny(subTagName, tagName)) {
				// 包含要设置宽度的标签
				Attributes attributes = subE.attributes();
				// 1.设置style内的宽度,优先级最高
				String style = attributes.get("style");
				if (StringUtils.isNotBlank(style)) {
					if (force) {
						style = style.replaceAll("width\\s*:[^px]+px", "width:" + (width - 40) + "px");
					} else {
						style = style.replaceAll("width\\s*:[^px]+px", "width:100%");
					}
					style = style.replaceAll("height\\s*:[^px]+px", "height:auto");
					attributes.put("style", style);
				}
				// 3.如果是系统图片，设置图片压缩路径
				if ("img".equals(subTagName)) {
					String imgSrc = attributes.get("src");
					boolean systemImg = imgSrc.indexOf("images.yamimap.com") > -1;
					if (systemImg && width > 0) {
						imgSrc += "?imageView2/2/q/100/interlace/1/w/" + (width - 40);
						attributes.put("src", imgSrc);
					} else if (!systemImg) {
						if (force) {
							attributes.put("width", width - 40 + "px");
							attributes.put("height", "auto");
						} else {
							attributes.put("width", "100%");
							attributes.put("height", "auto");
						}

					}
				}
				// 4.如果是行内元素，折行显示
				if (StringUtils.containsAny(subTagName, rowTags)) {
					addAtrWordWarp(attributes);
				}
			}
			// 获取子类继续
			resize(subE, width, force, tagName);
		}
	}

	/**
	 * 设置宽度
	 *
	 * @param e
	 * @param width   要设置的宽度
	 * @param tagName
	 */
	private static void resize(Element e, int width, String... tagName) {
		// 不包含，获取子类继续
		Elements allElements = e.children();
		for (Element subE : allElements) {
			String subTagName = subE.tagName();
			if (StringUtils.containsAny(subTagName, tagName)) {
				// 包含要设置宽度的标签
				Attributes attributes = subE.attributes();
				// 1.设置style内的宽度,优先级最高
				String style = attributes.get("style");
				if (StringUtils.isNotBlank(style)) {
					style = style.replaceAll("width\\s*:[^px]+px", "width:100%");
					style = style.replaceAll("height\\s*:[^px]+px", "height:auto");
					attributes.put("style", style);
				}
				// 3.如果是系统图片，设置图片压缩路径
				if ("img".equals(subTagName)) {
					String imgSrc = attributes.get("src");
					boolean systemImg = imgSrc.indexOf("images.yamimap.com") > -1;
					if (systemImg && width > 0) {
						imgSrc += "?imageView2/2/q/100/interlace/1/w/" + width;
						attributes.put("src", imgSrc);
					} else if (!systemImg) {
						String sourceWidth = attributes.get("width");
						if (StringUtils.isNotBlank(sourceWidth)) {
							attributes.put("width", "100%");
						}
						String sourcHeight = attributes.get("height");
						if (StringUtils.isNotBlank(sourcHeight)) {
							attributes.put("height", "auto");
						}
					}
				}
				// 4.如果是行内元素，折行显示
				if (StringUtils.containsAny(subTagName, rowTags)) {
					addAtrWordWarp(attributes);
				}
			}
			// 获取子类继续
			resize(subE, width, tagName);
		}
	}


	/**
	 * 添加折行显示属性
	 *
	 * @param attributes
	 */
	private static void addAtrWordWarp(Attributes attributes) {
		String style = attributes.get("style");
		if (StringUtils.isBlank(style)) {
			style = "";
		}
		style = style + ";word-wrap:break-word;word-break:normal;";
		attributes.put("style", style);
	}

	/**
	 * 移除Hmtl标签
	 *
	 * @param htmlStr
	 * @return
	 */
	public static String removeHtmlTag(String htmlStr) {
		if (StringUtils.isBlank(htmlStr)) {
			return htmlStr;
		}
		return Jsoup.parse(htmlStr).text();
	}


}
